package View;

import Model.TestVolume;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Scrollbar;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.*;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.ourbunny.jshout.Shouter;

import Controller.PlayerController;
import Model.CurrentPlayList;
import Model.CustomTextField;
import Model.Equalizer;
import Model.Nummer;
import Model.PlayerModel;

public class PlayerView extends JFrame implements Observer {

	private static final long serialVersionUID = 1L;
	private static PlayerView instance;
	
	private static JPanel pnlSongs = new JPanel();
	private static JPanel pnlAlbums = new JPanel();

	public static JButton btnPlay = new JButton();
	public static JButton btnStop = new JButton();
	public static JButton btnBrowse = new JButton();
	public static JButton btnForward = new JButton();
	public static JButton btnBackward = new JButton();
	public static final TestVolume setVolume = new TestVolume(SwingConstants.HORIZONTAL, 0.3f, 0.1f, 1.0f, true);
	
	public static JButton btnAddCurrentPlaylist = new JButton();
	public static JButton btnRemoveCurrentPlaylist = new JButton();
	public static JButton btnAddCurrentAlbumPlaylist = new JButton();
	public static JButton btnRemoveCurrentAlbumPlaylist = new JButton();
	public static JButton btnDeleteSongFormPlaylist = new JButton();
	
	public static ArrayList<Nummer> currentPlayList = new ArrayList<Nummer>();
	
	public static JTable tbSongs = new JTable();
	public static JTable tbAlbums = new JTable();
	public static JTable tbCurrentSongs = new JTable();
	public static JTable tbCurrentAlbums = new JTable();
	
	public static JLabel titel;
	public static JLabel artiest;
	public static JLabel album;
	public static JLabel jaar;
	public static JLabel tijdsduur;
	public static JLabel secondstimer;

	public static JList<String> lstSongs = new JList<String>();
	public static DefaultListModel<String> modelSongs = new DefaultListModel<String>();

	public static JProgressBar sldTimeLine = new JProgressBar();
	Color colorg = new Color(86,86,86);
	
	public static CustomTextField search = new CustomTextField(20);
	
	ArrayList<Nummer> songs = new ArrayList<Nummer>();
	
	public static Equalizer visual;
	
	public static DefaultTableModel mdlSongs = new DefaultTableModel(new Object[][] { 
	},new String[] {"Name", "Artist","Album"});
	public static DefaultTableModel mdlCurrentPLSongs = new DefaultTableModel(new Object[][] { 
	},new String[] {"Name", "Artist"});
	public static DefaultTableModel mdlAlbums = new DefaultTableModel(new Object[][]{		
	}, new String[] {"Name", "Artist", "Year"});
	public static DefaultTableModel mdlCurrentPLAlbums = new DefaultTableModel(new Object[][]{		
	}, new String[] {"Name", "Artist", "Year"});

	public static PlayerView getInstance() {
		if (instance == null) {
			instance = new PlayerView();
		}
		return instance;
	}

	public PlayerView() {
		ImageIcon img = new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/loading.png")));
		setIconImage(img.getImage());
		
		JPanel panel = new JPanel();
		JPanel pnlButtonPane = new JPanel();
		JPanel pnlHeader = new JPanel();
		JTabbedPane tabbedpane = new JTabbedPane();

		JScrollPane srollList = new JScrollPane();
		srollList.setBounds(400, 20, 362, 400);

		pnlSongs.setLayout(null);
		pnlAlbums.setLayout(null);
		
		panel.setPreferredSize(new Dimension(750, 410));
		panel.setLayout(null);

		pnlButtonPane.setBounds(0, 280, 550, 300);
		pnlButtonPane.setLayout(null);
		pnlButtonPane.setBackground(Color.DARK_GRAY);
		
		pnlHeader.setBounds(0, 0, 780, 20);
		pnlHeader.setLayout(null);
		pnlHeader.setBackground(Color.DARK_GRAY);
		
		search.setBounds(400, 0, 300, 20);
		search.addKeyListener(new PlayerController());
		search.setBackground(colorg);
		search.setPlaceholder("Search");
		
		lstSongs.addKeyListener(new PlayerController());
		lstSongs.setBackground(Color.lightGray);
		lstSongs.setFont(new Font("Segoe UI", Font.PLAIN, 14));
		lstSongs.setModel(modelSongs);
		lstSongs.addMouseListener(new MouseAdapter() {
		    public void mouseClicked(MouseEvent evt) {
		        if (evt.getClickCount() == 2) {
		           PlayerModel.getInstance().doubleClick();
		        }
		    }
		});

		ListSelectionModel listSelectionModel = lstSongs.getSelectionModel();
		listSelectionModel.addListSelectionListener(new Model.SharedListSelectionHandler());
		
		btnPlay.setBounds(140, 30, 50, 40);
		btnPlay.setText(">");
		btnPlay.addActionListener(new PlayerController());

		btnStop.setBounds(195, 30, 50, 40);
		btnStop.setText("[ ]");
		btnStop.addActionListener(new PlayerController());

		btnBrowse.setBounds(10,310,80,40);
		btnBrowse.setText("browse");
		btnBrowse.addActionListener(new PlayerController());
		
		sldTimeLine.setBounds(0, 0, 400, 20);
		sldTimeLine.setBackground(Color.DARK_GRAY);
		sldTimeLine.addChangeListener(new PlayerController());
	
		btnDeleteSongFormPlaylist.setBounds(700, 0, 62, 20);
		btnDeleteSongFormPlaylist.setText("X");
		btnDeleteSongFormPlaylist.addActionListener(new PlayerController());
	
		btnForward.setBounds(250, 30, 50, 40);
		btnForward.setText(">>");
		btnForward.addActionListener(new PlayerController());

		btnBackward.setBounds(85, 30, 50, 40);
		btnBackward.setText("<<");
		btnBackward.addActionListener(new PlayerController());
		
		setVolume.setBounds(85, 75, 220, 50);
		setVolume.setBackground(Color.DARK_GRAY);
		setVolume.addChangeListener(new PlayerController());

		titel = new JLabel("Please double click on a song to PLAY!");
		titel.setFont(new Font("Segoe UI", Font.BOLD, 10));
		titel.setBounds(20, 5, 300, 10);
		titel.setForeground(Color.WHITE);
		
		tijdsduur = new JLabel("0:00");
		tijdsduur.setBounds(320, 305, 300, 40);
		tijdsduur.setForeground(Color.RED);
		tijdsduur.setFont(new Font("Segoe UI", Font.PLAIN, 26));
		
		secondstimer = new JLabel("0:00");
		secondstimer.setBounds(15, 305, 300, 40);
		secondstimer.setForeground(Color.RED);
		secondstimer.setFont(new Font("Segoe UI", Font.PLAIN, 26));

		visual = new Equalizer();
		visual.init();
		visual.setBounds(0, 20, 399, 260);

		pnlHeader.add(titel);
		panel.add(tijdsduur);
		panel.add(secondstimer);
		panel.add(visual);

		add(panel);
		add(tabbedpane);

		panel.add(srollList);
		srollList.getViewport().add(lstSongs);

		tabbedpane.add("Player", panel);
		tabbedpane.add("Songs", pnlSongs);
		tabbedpane.add("About", pnlAlbums);

		setTitle("Solid Kangaroo - MP3 Speler");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		panel.add(pnlButtonPane);
		panel.add(pnlHeader);
		setLocationRelativeTo(null);

		pnlButtonPane.add(sldTimeLine);
		pnlButtonPane.add(btnPlay);
		pnlButtonPane.add(btnStop);
		pnlButtonPane.add(btnForward);
		pnlButtonPane.add(btnBackward);
		pnlButtonPane.add(setVolume);
	
		pnlHeader.add(search);
		pnlHeader.add(btnDeleteSongFormPlaylist);

		pack();

		setResizable(false);
		setLocationRelativeTo(null);

		addLibrary();
	}

	// Plaats hier je Library shit:

	private void addLibrary() {
     	JScrollPane scrSongs = new JScrollPane();

		btnAddCurrentPlaylist.setBounds(100,310,80,40);
		btnAddCurrentPlaylist.setText("add");
		btnAddCurrentPlaylist.addActionListener(new PlayerController());
		pnlSongs.add(btnAddCurrentPlaylist);
		
		btnRemoveCurrentPlaylist.setBounds(190,310,80,40);
		btnRemoveCurrentPlaylist.setText("remove");
		btnRemoveCurrentPlaylist.addActionListener(new PlayerController());
		pnlSongs.add(btnRemoveCurrentPlaylist);

		tbSongs.setSize(400, 300);
		tbSongs.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		tbAlbums.setSize(280, 300);

		scrSongs.setSize(400, 300);
		
		//playList
		JScrollPane scrCurrentSongs = new JScrollPane();
		
		tbCurrentSongs.setBounds(430,0,320,300);
		tbCurrentSongs.setFont(new Font("Segoe UI", Font.PLAIN, 12));
		scrCurrentSongs.setBounds(430,0,320,300);
		scrCurrentSongs.getViewport().add(tbCurrentSongs);
		
		pnlSongs.add(scrCurrentSongs);
		
		scrSongs.getViewport().add(tbSongs);

		scrSongs.repaint();

		pnlSongs.add(scrSongs);
		pnlSongs.add(btnBrowse);
		
		//about
		JTextPane textabout = new JTextPane();
		
		String IP;
		try {
			IP = InetAddress.getLocalHost().getHostAddress();
			String IPnieuw = IP.toString();
			textabout.setText("About,\n\nDe Solid Kangaroo - Muziekspeler is een MP3-speler gemaakt door\nNick van Endhoven, Jos Rijborz, Kasper van Rotterdam, Armando van Oeffelen en Stevie Blommers \n\n\nREMOTE CONTROL\n\nJe kan de Solid Kangaroo - Muziekspeler op afstand bedienen op "+IPnieuw+":4321");
		} catch (UnknownHostException e) {

		}
		
		textabout.setEditable(false);
		textabout.setBounds(50, 50, 400, 300);
		textabout.setFont(new Font("Segoe UI", Font.PLAIN, 15));
		textabout.setOpaque(false);
    
		pnlAlbums.add(textabout);
	}
	

	@Override
	public void update(Observable o, Object arg) {
		// TODO Add Observer
	}

}
